package com.crm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.ContractPageResponse;
import com.crm.base.PageResponse;
import com.crm.domain.entity.Contract;
import com.crm.domain.entity.ContractParam;
import com.crm.mapper.ContractMapper;
import com.crm.service.ContractService;

import com.crm.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@RequiredArgsConstructor
@Transactional
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    private final ContractMapper contractMapper;
    @Override
    public ContractPageResponse<Contract> list(ContractParam conreactParam, LocalDate beginCreateTime, LocalDate endCreateTime) {

        //用LambdaQueryWrapper实现分页条件查询
        LambdaQueryWrapper<Contract> wrapper = new QueryWrapper<Contract>().lambda()
                .like(ObjectUtil.isNotEmpty(conreactParam.getName()), Contract::getName, conreactParam.getName())
                .like(ObjectUtil.isNotEmpty(conreactParam.getContractNo()), Contract::getContractNo, conreactParam.getContractNo())
                .like(ObjectUtil.isNotEmpty(conreactParam.getPhone()), Contract::getPhone, conreactParam.getPhone())
                .like(ObjectUtil.isNotEmpty(conreactParam.getSubject()), Contract::getSubject, conreactParam.getSubject())
                .between(ObjectUtil.isNotEmpty(beginCreateTime) && ObjectUtil.isNotEmpty(endCreateTime),
                        Contract::getCreateTime, beginCreateTime, endCreateTime)
                .orderByDesc(Contract::getCreateTime);

        // 分页查询
        Page<Contract> page = new Page<>(conreactParam.getPageNum(), conreactParam.getPageSize());
        contractMapper.selectPage(page, wrapper);
        return ContractPageResponse.of(page);
    }
}
